﻿// (C) SocketCoder.Com 
// SocketCoder Silverlight Media Streaming 
// Last Modify: 31/5/2014
// Note: This Project is just some simple samples, if you are looking for a production solution Visit 
// SocketCoder.Com to Buy The Full WCS Web Conferencing Solution Source Code. 
//
namespace MediaStreaming
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;

    public class MemoryAudioSink : AudioSink
    {
        public event EventHandler OnBufferFulfill;

        AudioFormat _format;
        SpeexEncoderDecoder _speexencoder = new SpeexEncoderDecoder();

        public bool StartSending { get; set; }

        private int _VoiceQuality = 5;
        public int VoiceQuality
        {
            set
            {
                _VoiceQuality = this._speexencoder.VoiceQuality = value;
            }
            get
            {
                return _VoiceQuality;
            }
        }

        protected override void OnCaptureStarted()
        {
            _format = this.CaptureSource.AudioCaptureDevice.DesiredFormat;
        }

        protected override void OnCaptureStopped() { }

        protected override void OnFormatChange(AudioFormat Format) { }

        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {
            if (OnBufferFulfill != null & (sampleData.Length >= _format.SamplesPerSecond * _format.Channels) & StartSending)
            {
                byte[] EncodedBuffer = _speexencoder.Encode(sampleData);
                OnBufferFulfill(EncodedBuffer, null);
            }
        }
    }
}